﻿using Newtonsoft.Json.Linq;
using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Pay.Token.Controllers {
    /// <summary>
    /// 银行卡
    /// </summary>
    [RoutePrefix("bankcard")]
    public class BankCardController : BaseController {
        private IMerchantBankCardService _iMerchantBankCardService;

        public BankCardController(IMerchantBankCardService iMerchantBankCardService) {
            _iMerchantBankCardService = iMerchantBankCardService;
        }

        /// <summary>
        /// 获得当前商户审核通过的银行卡（用于判断是否允许提现、转赠红钻、商品兑换）
        /// </summary>
        /// <returns></returns>
        [Route("get_approved_bank_card"), HttpGet]
        public ResultMessage<api_merchant_bank_card_dto> GetApprovedBankCard() {
            var result = _iMerchantBankCardService.GetApprovedBankCard(UserId);
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 获取银行下拉列表
        /// </summary>
        /// <returns></returns>
        [Route("get_bank_list"), HttpGet,AllowAnonymous]
        public ResultMessage<List<DropdownItem>> GetBankList() {
            var result = typeof(BankCode).GetDropdownItemList();
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 通过Id获得银行卡
        /// </summary>
        /// <returns></returns>
        [Route("get_bank_card_by_id"), HttpGet]
        public ResultMessage<api_merchant_bank_card_dto> GetBankCardById([FromUri] JObject id) {
            var value = GetJsonValue<long>("id", id);
            var result = _iMerchantBankCardService.GetBankCardById(value);
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 获得当前商户的银行卡（包括未审核、审核不通过）
        /// </summary>
        /// <returns></returns>
        [Route("get_bank_card"), HttpGet]
        public ResultMessage<api_merchant_bank_card_dto> GetBankCard() {
            var result = _iMerchantBankCardService.GetBankCardByMerchantId(UserId);
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 新增或修改银行卡
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("add_or_update"), HttpPost]
        public ResultMessage<bool> AddOrUpdateBankCard([FromBody]edit_merchant_bank_card_dto model) {
            if (!ModelState.IsValid)
                return ResultToJson.Json<bool>(ModelError);
            var result = _iMerchantBankCardService.AddOrUpdateBankCard(model, UserId);
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 删除银行卡
        /// </summary>
        /// <returns></returns>
        [Route("remove_bank_card_by_id"), HttpPost]
        public ResultMessage<bool> RemoveBankCard([FromBody] JObject id) {
            var value = GetJsonValue<long>("id", id);
            var result = _iMerchantBankCardService.RemoveBankCard(value);
            return ResultToJson.Json(result);
        }
    }
}

